<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table,td{
            border: 1px solid #999;
            border-collapse: collapse;
        }
        td{
            width: 150px;
            height: 30px;
        }
        input[type=text]{
            height: 27px;
        }
        .c3{
            font-size: 12px;
            color: #999;
        }
        .success{
            font-size: 12px;
            color: green;
        }
        .error{
            font-size: 12px;
            color: red;
        }
    </style>
</head>
<body>
    <div class="outer">
        <input type="text" name="" id="num" value="10">
        <input type="button" value="出题" onclick="create()">
        <input type="button" value="验证" id="ck_btn" disabled onclick="check()">
    </div>

    <div id="content">
        
    </div>
    <script>
        function create(){
            //1. 获取题目的数量
            let num = document.querySelector("#num").value;
            //2. 创建一个数组
            let signs = ["+","-"];
            let div = document.querySelector("#content");

            let str = "<table>";

            for(let i=0;i<num;i++){
                //生成表达式
                let expre = parseInt(Math.random()*100+1) + signs[parseInt(Math.random()*signs.length)]+parseInt(Math.random()*100+1);

                str+=`
                <tr>
                    <td class='c1'>${expre}</td>
                    <td><input type="text"></td>
                    <td><span class='c3'>*</span></td>
                </tr>
                `;
            }

            str+="</table>";
            div.innerHTML=str;

            let btn_ck = document.querySelector("#ck_btn");
            btn_ck.disabled=false;

        }

        function check(){
            //1. 获取所有的第一列
            let tds = document.querySelectorAll(".c1");
            //2. 遍历
            for(let i=0;i<tds.length;i++){
                //2-1. 计算的结果
                let result = eval(tds[i].innerHTML.trim());
                //2-2 获取和tds[i]对应的文本框中的数据
                //let value=tds[i].nextElementSibling.children[0].value;
                let value = tds[i].nextElementSibling.getElementsByTagName("input")[0].value;
                
                //拿到span对象
                let span = tds[i].nextElementSibling.nextElementSibling.children[0];

                //2-3. 进行一个比较
                if(result == value){
                    span.className="success";
                    span.innerHTML="* √";
                }else{
                    span.className="error";
                    span.innerHTML="* "+result;
                }
            }
        }
    </script>
</body>
</html>